//
//  YCConsultViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/27.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  驾校客服咨询

#import "YCConsultViewController.h"
#import "YCServiceButton.h"
#import "LXActionSheet.h"

@interface YCConsultViewController ()

/** actionsheet */
@property (nonatomic, strong) UIButton *actionBackView;

@end

@implementation YCConsultViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self makeView];
}


- (void)makeView
{
    // 服务热线
    UIImageView *serviceImageView = [[UIImageView alloc] init];
    [self.view addSubview:serviceImageView];
    serviceImageView.image = [UIImage imageNamed:@"服务热线"];
    
    // 拨打客服电话
    YCServiceButton *callServiceButton = [YCServiceButton buttonWithType:UIButtonTypeCustom];
    [callServiceButton setImage:[UIImage imageNamed:@"拨打客服电话"] forState:UIControlStateNormal];
    callServiceButton.backgroundColor = [UIColor yc_colorWithRed:86 Green:200 Blue:14];
    [callServiceButton setTitle:@"拨打客服电话" forState:UIControlStateNormal];
    [callServiceButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [callServiceButton addTarget:self action:@selector(callServiceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:callServiceButton];
    
    // 在线客服咨询
    YCServiceButton *onlineServiceButton = [YCServiceButton buttonWithType:UIButtonTypeCustom];
    [onlineServiceButton setImage:[UIImage imageNamed:@"在线联系客服"] forState:UIControlStateNormal];
    onlineServiceButton.backgroundColor = [UIColor whiteColor];
    onlineServiceButton.layer.borderColor = [UIColor yc_colorWithUInt:0x4d4d4d alpha:0.5].CGColor;
    onlineServiceButton.layer.borderWidth = 1;
    [onlineServiceButton setTitle:@"在线客服咨询" forState:UIControlStateNormal];
    [onlineServiceButton setTitleColor:[UIColor yc_colorWithUInt:0x4d4d4d] forState:UIControlStateNormal];
    [onlineServiceButton addTarget:self action:@selector(onlineServiceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:onlineServiceButton];
    
    //布局
    [serviceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(heightFromIphone6Height(150));
        make.centerX.equalTo(self.view);
        make.width.equalTo(@130);//260*204
        make.height.equalTo(@102);
    }];
    
    [callServiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(35);
        make.bottom.equalTo(self.view).offset(-49);
        make.width.equalTo(@130);//284px
        make.height.equalTo(@35);
    }];
    
    [onlineServiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-35);
        make.bottom.equalTo(self.view).offset(-49);
        make.width.equalTo(@130);//284px
        make.height.equalTo(@35);
    }];

}

#pragma mark - 点击事件
- (void)callServiceBtnClick:(UIButton *)button
{
    YCLogFunc
    /************ 手写actionsheet ************/
//    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    backButton.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.9];
//    [backButton addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view.window addSubview:backButton];
//    
//    self.actionBackView = backButton;
//    
//    
//    UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 100)];
//    actionView.backgroundColor = [UIColor yc_colorWithUInt:0xf0f0f0];
//    [backButton addSubview:actionView];
//    // 拨打
//    UIButton *callButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,kScreenWidth, 50)];
//    callButton.titleLabel.font = [UIFont systemFontOfSize:17];
//    callButton.backgroundColor = [UIColor whiteColor];
//    [callButton setTitleColor:[UIColor yc_colorWithUInt:0x00b4ff] forState:UIControlStateNormal];
//    [callButton setTitle:@"拨打 400-0125-111" forState:UIControlStateNormal];
//    [callButton addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];
//    [actionView addSubview:callButton];
//    
//    // 取消
//    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0,57, kScreenWidth, 43)];
//    cancelButton.titleLabel.font = [UIFont systemFontOfSize:17];
//    cancelButton.backgroundColor = [UIColor whiteColor];
//    [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
//    [cancelButton addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [actionView addSubview:cancelButton];
//    
//    [UIView animateWithDuration:0.35 animations:^{
//        actionView.frame = CGRectMake(0, kScreenHeight - 100, kScreenWidth, 100);
//    }];
    /************ 封装成独立actionsheet ************/
    // 用封装的actionSheet
    LXActionSheet *actionSheet = [[LXActionSheet alloc] initWithCancelButtonTitle:@"取消" otherButtonTitlesArray:@[@"拔打 400-0125-111"]];
    UIButton *callButton =[actionSheet buttonAtIndex:1];
    callButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [callButton setTitleColor:[UIColor yc_colorWithUInt:0x00b4ff] forState:UIControlStateNormal];
    [actionSheet showInView:self.view mask:YES maskColor:[UIColor colorWithWhite:0.3 alpha:1]  click:^(NSUInteger index, LXActionSheet *sheet) {
        if (index == 1) {
            [self callPhone];
        }
    } cancel:^(NSUInteger index, LXActionSheet *sheet) {
        
    }];
}

- (void)onlineServiceBtnClick:(UIButton *)button
{
    YCLogFunc
    LXActionSheet *sheet = [[LXActionSheet alloc] initWithCancelButtonTitle:@"取消" otherButtonTitlesArray:@[@"是",@"否"]];
    [sheet showInView:self.view mask:YES click:^(NSUInteger index, LXActionSheet *sheet) {
        YCLog(@"btn click:%zd",index);
    } cancel:^(NSUInteger index, LXActionSheet *sheet) {
        YCLog(@"LXActionSheet cancel:%zd",index);
    }];
}

- (void)callPhone
{
    YCLogFunc
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://4000125111"]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4000125111"]];
    }
//    [self cancelBtnClick];
}

- (void)cancelBtnClick
{
    [self.actionBackView removeFromSuperview];
}

@end
